package lab03.test08;


import jdk.nashorn.internal.ir.WhileNode;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**8、	将文件一和文件二的内容合并成文件三，合并方法是：从文件一读取一个字节放入文件三，
 * 再从文件二读取一个字节放入文件三，
 * 如此轮流直至某一个文件读完，再将较长文件中的剩余部分读取放入至文件三
 * Created by dell on 2017/12/7.
 */
public class Test08 {

    public static void main(String[] args) throws FileNotFoundException {
        String file1 = "F:\\one.txt";
        String file2 = "F:\\two.txt";
        String file3 = "F:\\three.txt";

        FileInputStream fileInputStream = null;
        FileInputStream fileInputStreamnext = null;

        FileOutputStream fileOutputStream = null;

        try {
            fileInputStream = new FileInputStream(file1);
            fileInputStreamnext = new FileInputStream(file2);

            fileOutputStream = new FileOutputStream(file3);
            int line;
            int linenext;
            int a;
            while ((a = (line = fileInputStream.read()) >
                    (linenext = fileInputStreamnext.read()) ? line : linenext) != -1) {
                fileOutputStream.write((char) line);
                fileOutputStream.write((char) linenext);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileInputStream.close();
                fileInputStreamnext.close();
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}




